package com.macro.mall.portal.controller;

import com.google.common.collect.Maps;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.product.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;
import java.util.Map;

/**
 */
@Slf4j
@RestController
@Api(tags = "AppProdSpecController", description = "App服务接口")
@RequestMapping("/appProdSpec")
public class AppProdSpecController {


    @Autowired
    private RestTemplate restTemplate;

    @Value("${host.mall.product}")
    private String HOST_MALL_PRODUCT;
    @Value("${host.mall.order}")
    private String HOST_MALL_ORDER;

    private String SERVICE_NAME="/appProdSpec";

    @ApiOperation(value = "APP商品服务列表")
    @RequestMapping(value = "/listPage", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmProdSpecDto>> listPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                            @RequestParam(value = "searchName", defaultValue = "") String searchName
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/listPage";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("pageNum",pageNum);
        param.put("pageSize",pageSize);
        param.put("searchName",searchName);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★AppProdSpecController.listPage   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "APP单个服务简介")
    @RequestMapping(value = "/getSpecDtail", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<String> getSpecDtail(@RequestParam(value = "prodSpecId") Long prodSpecId
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/getSpecDtail";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("prodSpecId",prodSpecId);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★AppProdSpecController.getSpecDtail   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "APP根据品类包获取已发布商品属性集合")
    @RequestMapping(value = "/getSpecPrpty", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmPrptyDto>> getSpecPrpty(@RequestParam(value = "packageId") String packageId,
                                                       @RequestParam(value = "prodSpecId") Long prodSpecId
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/getSpecPrpty";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("packageId",packageId);
        param.put("prodSpecId",prodSpecId);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★AppProdSpecController.getSpecPrpty   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class, packageId,prodSpecId);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "APP根据选择属性查询满足条件场馆的地点")
    @RequestMapping(value = "/getProdSite", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<List<Map<String,Object>>> getProdSite(@RequestBody QueryProdSiteDto queryDto, BindingResult result
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/getProdSite";
        ResponseEntity<CommonResult> responseEntity = restTemplate.postForEntity(url, queryDto, CommonResult.class);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "APP获取库存")
    @RequestMapping(value = "/getServerStock", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<ReturnServiceStockDto> getServerStock(@RequestParam(value = "prodId") Long prodId,
                                                              @RequestParam(value = "skuInstId") Long skuInstId,
                                                              @RequestParam(value = "siteId") Integer siteId
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/getServerStock";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("prodId",prodId);
        param.put("skuInstId",skuInstId);
        param.put("siteId",siteId);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★AppProdSpecController.getServerStock   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class, prodId,skuInstId,siteId);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "获取单个产品")
    @RequestMapping(value = "/getProductById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmProductDto> getProductById(@RequestParam(value = "prodId", defaultValue = "") Long prodId
    ) {

        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/getProductById";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("prodId",prodId);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★AppProdSpecController.getProductById   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class, prodId);

        return responseEntity.getBody();
    }
    @ApiOperation(value = "APP获取库存-上门服务")
    @RequestMapping(value = "/getServerStockByDoor", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ReturnServiceStockDto> getServerStockByDoor(@RequestBody QueryStockDoorDto queryDto, BindingResult result
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/getServerStockByDoor";
        ResponseEntity<CommonResult> responseEntity = restTemplate.postForEntity(url, queryDto, CommonResult.class);
        return responseEntity.getBody();

    }
    @ApiOperation(value = "获取单个服务规格")
    @RequestMapping(value = "/getProdSpecById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmProductDto> getProdSpecById(@RequestParam(value = "prodSpecId", defaultValue = "") Long prodSpecId
    ) {

        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/getProdSpecById";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("prodSpecId",prodSpecId);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★AppProdSpecController.getProdSpecById   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class, prodSpecId);

        return responseEntity.getBody();
    }
    @ApiOperation(value = "根据商品标识获取商品规格")
    @RequestMapping(value = "/getProdSpecByProdId", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmProductDto> getProdSpecByProdId(@RequestParam(value = "prodId", defaultValue = "") Long prodId
    ) {

        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/getProdSpecByProdId";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("prodId",prodId);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★AppProdSpecController.getProdSpecByProdId   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class, prodId);

        return responseEntity.getBody();
    }
}
